﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="NonMatrixTypeRegistrationException.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Exceptions
{
    #region Usings

    using System;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Security;

    using ExulLibrary.Mathematics.LinearAlgebra.Properties;

    #endregion

    /// <summary>
    ///     Describes an error for case when a type provided to register as a matrix is not inherited
    ///     from the expected base matrix type.
    /// </summary>
    [Serializable]
    public sealed class NonMatrixTypeRegistrationException : ArgumentException
    {
        #region Constructors and Destructors

        #region public

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonMatrixTypeRegistrationException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="actualType">
        ///     The actual type passed to register.
        /// </param>
        /// <param name="expectedBaseType">
        ///     The expected base type for a type to register.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="actualType"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="expectedBaseType"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        public NonMatrixTypeRegistrationException(string paramName, Type actualType, Type expectedBaseType)
            : this(paramName, actualType, expectedBaseType, null)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonMatrixTypeRegistrationException"/> class.
        /// </summary>
        /// <param name="paramName">
        ///     The parameter name.
        /// </param>
        /// <param name="actualType">
        ///     The actual type passed to register.
        /// </param>
        /// <param name="expectedBaseType">
        ///     The expected base type for a type to register.
        /// </param>
        /// <param name="innerException">
        ///     The inner exception.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="actualType"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="expectedBaseType"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        public NonMatrixTypeRegistrationException(
            string paramName, Type actualType, Type expectedBaseType, Exception innerException)
            : base(Resources.TypeIsNotDescendantOfMatrix, paramName, innerException)
        {
            if (actualType == null)
            {
                throw new ArgumentNullException("actualType");
            }

            if (expectedBaseType == null)
            {
                throw new ArgumentNullException("expectedBaseType");
            }

            this.ActualType = actualType;
            this.ExpectedBaseType = expectedBaseType;
        }

        #endregion

        #region private

        /// <summary>
        ///     Initializes a new instance of the <see cref="NonMatrixTypeRegistrationException"/> class.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        private NonMatrixTypeRegistrationException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.ActualType = (Type)info.GetValue("ActualType", typeof(Type));
            this.ExpectedBaseType = (Type)info.GetValue("ExpectedBaseType", typeof(Type));
        }

        #endregion

        #endregion

        #region Properties

        #region public

        /// <summary>
        ///     Gets the actual type passed to register.
        /// </summary>
        /// <value>
        ///     The actual type passed to register.
        /// </value>
        public Type ActualType
        {
            get;
            private set;
        }

        /// <summary>
        ///     Gets the expected base type for a type to register.
        /// </summary>
        /// <value>
        ///     The expected base type for a type to register.
        /// </value>
        public Type ExpectedBaseType
        {
            get;
            private set;
        }

        #endregion

        #endregion

        #region Overridden Properties

        #region public

        /// <summary>
        ///     Gets the error message and the parameter name, or only the error message if no parameter name is set.
        /// </summary>
        /// <value>
        ///     A text string describing the details of the exception.
        /// </value>
        public override string Message
        {
            get
            {
                return string.Format(
                    CultureInfo.CurrentCulture,
                    base.Message,
                    this.ParamName,
                    this.ExpectedBaseType.FullName,
                    this.ActualType.FullName);
            }
        }

        #endregion

        #endregion

        #region Overridden Methods

        #region public

        /// <summary>
        ///     Sets the <see cref="SerializationInfo"/> object with the parameter name and additional exception information.
        /// </summary>
        /// <param name="info">
        ///     The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        ///     The contextual information about the source or destination.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="info"/> is <see langword="null"/>.
        /// </exception>
        [SecurityCritical]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            info.AddValue("ActualType", this.ActualType);
            info.AddValue("ExpectedBaseType", this.ExpectedBaseType);
        }

        #endregion

        #endregion
    }
}